package org.mokusakura.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.mokusakura.constant.RequestParameterNameConstant;
import org.mokusakura.constant.ServletUrlConstant;
import org.mokusakura.domain.Item;
import org.mokusakura.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GetItemController {
    public static final String ITEM_ID_TO_SEARCH = "itemId";
    @Autowired
    private CatalogService catalogService;

    @GetMapping(ServletUrlConstant.GET_ITEM_SERVLET_URL)
    public Object getItem(@RequestParam(RequestParameterNameConstant.JSON_STRING) String json) {
        JSONObject jsonObject = JSON.parseObject(json);
        String productId = jsonObject.getString(ITEM_ID_TO_SEARCH);
        Item item = catalogService.getItem(productId);
        return item;
    }
}
